
<template>
   <!-- 设置语言为中文 -->
   <el-config-provider :locale="locale">
      <router-view></router-view>
      <!-- AI助手组件 - 全局悬浮 -->
      <AiAssistant v-show="showAiAssistant" ref="aiAssistantRef" />
   </el-config-provider>
</template>

<script setup>
import { ref, computed, provide, onMounted, nextTick, watch } from 'vue'
import { useRoute } from 'vue-router'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import AiAssistant from '@/components/AiAssistant.vue'

const locale = zhCn
const route = useRoute()

// 判断是否显示AI助手（只在前台页面显示，不在后台管理页面显示）
const showAiAssistant = computed(() => {
    return !route.path.startsWith('/admin')
})

// AI助手组件引用
const aiAssistantRef = ref(null)

// 全局AI助手方法
const globalOpenAiAssistant = () => {
    console.log('App.vue: globalOpenAiAssistant 被调用')
    console.log('App.vue: aiAssistantRef.value:', aiAssistantRef.value)
    console.log('App.vue: showAiAssistant.value:', showAiAssistant.value)

    if (!aiAssistantRef.value) {
        console.log('App.vue: AI助手组件引用为空')
        return
    }

    if (typeof aiAssistantRef.value.openAiAssistant === 'function') {
        console.log('App.vue: 调用 aiAssistantRef.value.openAiAssistant()')
        aiAssistantRef.value.openAiAssistant()
    } else {
        console.log('App.vue: openAiAssistant 方法不存在或不是函数')
        console.log('App.vue: 可用方法:', Object.keys(aiAssistantRef.value || {}))
    }
}

const globalAddMessageToAiAssistant = (data) => {
    console.log('App.vue: globalAddMessageToAiAssistant 被调用，数据:', data)
    console.log('App.vue: aiAssistantRef.value:', aiAssistantRef.value)
    console.log('App.vue: showAiAssistant.value:', showAiAssistant.value)

    if (!aiAssistantRef.value) {
        console.log('App.vue: AI助手组件引用为空')
        return
    }

    if (typeof aiAssistantRef.value.addMessageToAiAssistant === 'function') {
        console.log('App.vue: 调用 aiAssistantRef.value.addMessageToAiAssistant()')
        aiAssistantRef.value.addMessageToAiAssistant(data)
    } else {
        console.log('App.vue: addMessageToAiAssistant 方法不存在或不是函数')
        console.log('App.vue: 可用方法:', Object.keys(aiAssistantRef.value || {}))
    }
}

// 提供全局AI助手方法
provide('openAiAssistant', globalOpenAiAssistant)
provide('addMessageToAiAssistant', globalAddMessageToAiAssistant)

console.log('App.vue: 已提供全局AI助手方法')

// 监听AI助手显示状态变化
watch(showAiAssistant, (newValue) => {
    console.log('App.vue: showAiAssistant 变化:', newValue)
    if (newValue) {
        nextTick(() => {
            console.log('App.vue: AI助手应该显示，检查组件状态')
            console.log('App.vue: aiAssistantRef.value:', aiAssistantRef.value)
        })
    }
}, { immediate: true })

// 组件挂载后检查AI助手组件状态
onMounted(() => {
    nextTick(() => {
        console.log('App.vue: 组件挂载完成，检查AI助手组件状态')
        console.log('App.vue: 当前路由:', route.path)
        console.log('App.vue: showAiAssistant:', showAiAssistant.value)
        console.log('App.vue: aiAssistantRef.value:', aiAssistantRef.value)
        if (aiAssistantRef.value) {
            console.log('App.vue: AI助手组件已准备好')
            console.log('App.vue: 可用方法:', Object.keys(aiAssistantRef.value))
        } else {
            console.log('App.vue: AI助手组件未准备好')
        }
    })
})

console.log(import.meta.env)
</script>

<style>
/* 自定义顶部加载 Loading 颜色 */
#nprogress .bar {
   background: #409eff!important;
}

/* 暗黑模式 body 背景色 */
.dark body {
   --tw-bg-opacity: 1;
    background-color: rgb(17 24 39 / var(--tw-bg-opacity));
}
</style>
